<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-03-29 23:07:47
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-06-05 21:57:52
 */

namespace app\services;

use Yii;
use yii\web\Response;
use app\models\InsurancePolicy;

/**
* 支付相关服务
*/
class PayServices
{
	// 正式地址
	const AGNENT_CODE = 'BA100162';
	const PAY_SERVICE_URL  = "https://eservice.allianz.cn/paymentgateway/payment";
	const PAY_PASSWORD_KEY = "d0e9f760558d5a0bb86301193b9966eb";
	const PAY_NOTIFY_URL = "http://m.funfind.cn/notify/order";
	const PAY_RETURN_URL = "http://m.funfind.cn/pay/return-order";
	// 同步消息存储给信封中的地址
    const ORDER_NEWS_PUSH_URL = "http://api.funfind.cn/user/al-order-news-push";

    // 测试地址
	// const AGNENT_CODE = 'AC000039';
	// const PAY_SERVICE_URL  = "https://eservice.allianz.cn/paymentgateway_test/payment";
	// const PAY_PASSWORD_KEY = "828f7f755bd7de957175f8e8b5301ccc";
	// const PAY_NOTIFY_URL = "http://funfind.local.isao123.com/pay/notify-order";
	// const PAY_RETURN_URL = "http://funfind.local.isao123.com/pay/return-order";
	
	const PAY_TYPE_ALIPAY = 'alipay';
	const PAY_TYPE_WAPALIPAY = 'wapalipay';
	const PAY_TYPE_BILL = '99bill';
	const PAY_TYPE_WXPAY = 'wxpay';

	public static $pay_type = [
		self::PAY_TYPE_ALIPAY => '支付宝',
		self::PAY_TYPE_WAPALIPAY => '手机支付宝',
		self::PAY_TYPE_BILL   => '快钱',
		self::PAY_TYPE_WXPAY  => '微信支付',
	];
	
	public function sendRequest($amount,$policyRef){
		$request = [
			"AgencyCode" 	=> self::AGNENT_CODE,
			"NotifyUrl"  	=> self::PAY_NOTIFY_URL,
			"PaymentMethod" => self::PAY_TYPE_WXPAY,
			"PolicyRef"  	=> $policyRef,
			"ReturnUrl"  	=> self::PAY_RETURN_URL,
			"TotalPremium"  => $amount
		];
		
		$request["Sign"] = $this->_makeSign($request);
		
		return $request;
	}
	
	/**
	 * 生成签名
	 */
	private function _makeSign($request){
		$temp = sprintf("agencyCode=%s&notifyUrl=%s&paymentMethod=%s&policyRef=%s&returnUrl=%s&totalPremium=%s",$request["AgencyCode"],$request["NotifyUrl"],$request["PaymentMethod"],$request["PolicyRef"],$request["ReturnUrl"],$request["TotalPremium"]);
		return md5($temp.self::PAY_PASSWORD_KEY);
	}

	/**
	 * 验证返回参数
	 */
	public function checkReturnSign($params){
		if (isset($params['sign'])) {
			unset($params['sign']);	
		}
		$response = $params;

		// 
		$sign_param = [
			"agencyCode" => $response['agencyCode'],
			"msg" => $response['msg'],
			"notifyUrl"  => $response['notifyUrl']
		];
		if (isset($response['payTime']) && $response['payTime']) {
			$sign_param['payTime'] = $response['payTime'];
		}
		if (isset($response['paymentGatewaySn']) && $response['paymentGatewaySn']) {
			$sign_param['paymentGatewaySn'] = $response['paymentGatewaySn'];
		}
		if (isset($response['paymentMethod']) && $response['paymentMethod']) {
			$sign_param['paymentMethod'] = $response['paymentMethod'];
		}
		
		$sign_param['policyRef']    = $response['policyRef'];
		$sign_param['returnUrl']    = $response['returnUrl'];
		$sign_param['totalPremium'] = $response['totalPremium'];
		$sign_param['tradeStatus']  = $response['tradeStatus'];
		
		return md5(http_build_query($sign_param).self::PAY_PASSWORD_KEY);
	}

}